if lib and not lib.getQuoteLevel2 then


function lib:SubscribeLevel2orders()
	local codes = {}
	
	local orders = self:GetTable("orders")
	--message("SubscribeLevel2orders orders " .. tostring(#orders))
	for i, v in ipairs(orders) do
		if bit.test(v.flags, 0) then
			codes[v.sec_code] = v.class_code
		end
	end
	
	orders = self:GetTable("stop_orders")
	--message("SubscribeLevel2orders stop_orders " .. tostring(#orders))
	for i, v in ipairs(orders) do
		if bit.test(v.flags, 0) then
			codes[v.sec_code] = v.class_code
		end
	end
	
	for sec_code, class_code in pairs(codes) do
		if self:SubscribeLevel2(sec_code, class_code) then
			--message("SubscribeLevel2orders " .. tostring(sec_code) .. " " .. tostring(class_code))
			
			codes[sec_code] = nil
		else
			message("SubscribeLevel2orders failed " .. tostring(sec_code) .. " " .. tostring(class_code))
		end
	end
	
	return codes
end


function lib:SubscribeLevel2(sec_code, class_code)
	if not class_code then
		class_code, sec_code = lib:GetClass(sec_code)
	end
	
	--message("SubscribeLevel2 " .. tostring(sec_code) .. " " .. tostring(class_code))
	
	if class_code and sec_code then
		if IsSubscribed_Level_II_Quotes(class_code, sec_code) then
			return true
		else
			local ret = Subscribe_Level_II_Quotes(class_code, sec_code)
			
			--message("SubscribeLevel2 " .. tostring(sec_code) .. " " .. tostring(class_code) .. " " .. tostring(ret))
			
			return ret
		end
	end
end


function lib:getQuoteLevel2(sec_code, class_code)
	if not class_code then
		class_code, sec_code = lib:GetClass(sec_code)
	end
	
	if class_code and sec_code then
		lib:SubscribeLevel2(sec_code, class_code)
		
		return getQuoteLevel2(class_code, sec_code)
	end
end


function lib:GetSpread(instrument, qty, is_long)
	qty = int(qty)
	local b = nil
	local a = nil
	local q = self:getQuoteLevel2(instrument)
	if type(q) == "table" and type(q.bid) == "table" and type(q.offer) == "table" then
		table.sort(q.bid, function(w1, w2)
			if (tonumber(w2.price) < tonumber(w1.price)) then
				return true
			end
		end)
		table.sort(q.offer, function(w1, w2)
			if (tonumber(w2.price) > tonumber(w1.price)) then
				return true
			end
		end)
		local n = 0
		for i, v in ipairs(q.bid) do
			n = n + int(v.quantity)
			if is_long == true or n >= qty then
				b = tonumber(v.price)
				break
			end
		end
		n = 0
		for i, v in ipairs(q.offer) do
			n = n + int(v.quantity)
			if is_long == false or n >= qty then
				a = tonumber(v.price)
				break
			end
		end
	else
		lib:SubscribeLevel2(instrument)
	end
	
	if b and a then
		return (a - b) / b, b, a
	end
end


end -- of file
